﻿var portals = {
    connect: 'http://localhost:1199/signalr/hubs',
    contenMessage: '#messages',
    txtMessage: '#textChat',
    room100: '#room100Id',
    room1000: '#room1000Id',
    room10000: '#room10000Id',
    back: '.back',
    spin: '#spin',
    auto:'#auto',
    max_conten: 100

};

(function (window) {
    function PortalEnginer() {
        this.initialize();
    }
    var p = PortalEnginer.prototype;

    p.initialize = function () {

        this.Connection = $.hubConnection(portals.connect);

        this.PortalHub = this.Connection.createHubProxy('gameHub');
        this.Connection.stateChanged(function (change) {
            if (change.newState === $.signalR.connectionState.connecting) {
                $(portals.txtMessage).attr('disabled', 'disabled');
                $(portals.txtMessage).attr("placeholder", "Đang kết nối...");
            }
            else if (change.newState === $.signalR.connectionState.reconnecting) {

                $(portals.txtMessage).attr('disabled', 'disabled');
                $(portals.txtMessage).attr("placeholder", "Đang kết nối...");
                
            }
            else if (change.newState === $.signalR.connectionState.connected) {
                console.info('connected');
            }
            else if (change.newState === $.signalR.connectionState.disconnected) {
                $(portals.txtMessage).attr('disabled', 'disabled');
                $(portals.txtMessage).attr("placeholder", "mất kết nối...");
            }
        });
        this.Connection.connectionSlow(function () {
            console.log('Connection Slow.');
        });

        this.Connection.error(function (error) {
            console.error('SignalR error: ', error);
        });

        var obj = new PortalHub(this.PortalHub);

        this.startPortal();
    };

    p.startPortal = function () {
        try {
            this.Connection.start().done(function() {
                portalEnginer.PortalHub.server.JoinPortal().done(function (result) {
                    if (result.isLogin) {
                        $(portals.txtMessage).removeAttr('disabled');
                        $(portals.txtMessage).attr("placeholder", "nhập nội dung...");
                        $(portals.txtMessage).keypress(function (event) {
                            if (event.which == 13) {
                                var msg = $(portals.txtMessage).val();
                                if (msg.length > 0)
                                    portalEnginer.PortalHub.server.Send(msg);
                                $(portals.txtMessage).val('');
                                $(portals.txtMessage).val('');
                                $(portals.txtMessage).focus();
                                
                            }
                        });
                        
                        $("#txtBet").keypress(function (event) {
                            if (event.which == 13) {
                                PortalCommon.setBet("1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20",function(data) {
                                    
                                });
                            }
                        });
                    } else {
                        $(portals.txtMessage).attr('disabled', 'disabled');
                        $(portals.txtMessage).attr("placeholder", "Bạn vui lòng đăng nhập!");
                    }

                    $(portals.contenMessage).empty();
                    for (var i = 0; i < result.messages.length; i++) {
                        var obj = result.messages[i];
                        $(portals.contenMessage).append('<p style="margin-bottom: 2px;"><span><b>' + obj.AccountName + '</b></span>: ' + obj.Message + ' </p>');
                    }
                    

                }).fail();
            }).fail();
        } catch(e) {
        }
    };

    p.restartPortal = function () {
        try {
            this.Connection.start().done(function () {

            }).fail();
        } catch (e) { }
    };

    window.PortalEnginer = PortalEnginer;
   

}(window));
 

(function () {
    var PortalHub = function (hub) {
        console.log(hub);
        hub.server = {};
        hub.client = {};

        $.extend(hub.server, {
            JoinPortal: function () {
                return hub.invoke.apply(hub, $.merge(["JoinPortal"], $.makeArray(arguments)));
            },
            Send: function () {
                return hub.invoke.apply(hub, $.merge(["Send"], $.makeArray(arguments)));
            },
        });
        hub.on('PublishMessage', function (result) {
            $(portals.contenMessage).append('<p><span>' + result.AccountName + ':</span>' + result.Message + '</p>');
            if ($(portals.contenMessage +' p').length > portals.max_conten)
                $($(portals.contenMessage + ' p').get(0)).remove();
            
            $("#Div1").mCustomScrollbar("update");
            $("#Div1").mCustomScrollbar("scrollTo", "bottom");

        });

        hub.on('PublishAward', function (result) {
            setTimeout(function () {
                var l = $('#vinhdanh p').length;
                if ($('#vinhdanh p').length > portals.max_conten)
                    $($('#vinhdanh p').get(l - 1)).remove();
                $('#vinhdanh').prepend('<p>' + result.AcountName + '<span style="color: #ffd800;"> thắng ' + PortalCommon.formatMoney(result.Money) + ' sao</span> lúc ' + PortalCommon.formDateTime(result.Date) + ' </p>');
                $("#Div2").mCustomScrollbar("update");
                $("#Div2").mCustomScrollbar("scrollTo", "top");
            }, 5000);

        });
        
        hub.on('PublishJacpot', function (result) {
            if (result!=undefined)
                $('#bangthuongId').html('<span style="float: right;margin: 12px 25px 0px 0px;-ms-transform: scale(0.8,0.8);-webkit-transform: scale(0.8,0.8);transform: scale(0.8,0.8)">' + PortalCommon.MoneyFont24(result[PortalCommon.RoomID - 1].Jackpot) + '</span>');

        });


    };
    window.PortalHub = PortalHub;
    
})(window, $);